# -*- coding: utf-8 -*-

import orjson
from datetime import datetime
from fastapi.encoders import jsonable_encoder
from .common import *


@add_serializer
class TemplateType(YgBase):
    __tablename__: str = "YG_TemplateType"
    __table_args__ = {'extend_existing': True}
    id: Mapped[int_pk_auto]
    code: Mapped[required_unique_name]
    name: Mapped[required_unique_name]
    category: Mapped[required_string]
    templates: Mapped[List["Template"]] = relationship(back_populates="template_type")

    def __repr__(self) -> str:
        return f'id: {self.id}, name: {self.name}'

    def __str__(self) -> str:
        return f'id: {self.id}, name: {self.name}'


@add_serializer
class Template(YgBase):
    __tablename__: str = "YG_Template"
    __table_args__ = {'extend_existing': True}
    id: Mapped[int_pk_auto]
    name: Mapped[required_unique_name]
    description: Mapped[required_string] = mapped_column(String(1000), default='', nullable=True)
    config: Mapped[str] = mapped_column(String(4000), nullable=False)
    template_type_id: Mapped[int] = mapped_column(ForeignKey('YG_TemplateType.id'))
    template_type: Mapped[TemplateType] = relationship(lazy=False, back_populates="templates")

    def __repr__(self) -> str:
        return f'id: {self.id}, name: {self.name}'

    def __str__(self) -> str:
        return f'id: {self.id}, name: {self.name}'
